<?php
include $_SERVER['DOCUMENT_ROOT'].'/phpprojects/conn.php';
$code = $_GET['code'];
$username = $_GET['username'];
$sql = "delete from qrcode where release_time+valid_time<".time();
mysql_query($sql);
$sql = "select * from qrcode natural join course where message='".$code."'";
$rs = mysql_query($sql);
$result = array();
if (mysql_num_rows($rs) == 0) {
    $result['status']=1;
    $result['token']="";
    $result['courseId']="1";
    $result["longtitude"] = "0";
	$result["latitude"] = "0";
	$result["validRange"] = "0";
} else {
	$src = "1234567890qwertyuiopasdfghjklzxcvbnm";
	$des = "";
	for ($i = 0; $i < 10; $i++) {
    	$rand = rand(0, 35);
    	$c = substr($src, $rand, 1);
    	$des = $des . $c;
	}

	$result['status']=0;
	$row = mysql_fetch_array($rs);
	$sql = "delete from token where course_id='".$row["course_id"]."' and name='$username'";
	// echo $sql;
	mysql_query($sql);
	$sql = "insert into token values('".$row["course_id"]."','$username',".time().",'$des')";
	// echo $sql;
	mysql_query($sql);
    $result['courseId']=$row["course_id"];
    $result['token']=$des;
	$result["longtitude"] = $row["longtitude"];
	$result["latitude"] = $row['latitude'];
	$result["validRange"] = $row['valid_range'];
}
echo json_encode($result);